package com.ljxt.dt.dao.storemanagementdao.impl;

import com.ljxt.dt.dao.storemanagementdao.RepertoryDao;
import com.ljxt.dt.domain.GoodsInfo;
import com.ljxt.dt.domain.Stock;
import com.ljxt.dt.domain.StockDetail;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.vo.storemanagement.RepertorySelectVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-20 9:54
 * @copyright 老九学堂
 */

public class RepertoryDaoImpl implements RepertoryDao {
    @Override
        public List<StockDetail> selectRepertory(RepertorySelectVO repertorySelectVO,Integer pageIndex,Integer pageSize) {
            String sql = "select * from stock_detail  where 1 = 1 ";
            List<Object> params = new ArrayList<>();
        //通过传入进来的对象动态得到sql

        //如果有商品名字则先查到名字对应的id
        if (repertorySelectVO.getGoodsName() != null){
            sql += "and goods_id = (select id  from goods_info where goods_name = ?)";
            params.add(repertorySelectVO.getGoodsName());
        }
        if(repertorySelectVO.getGoodsId() != null){
                sql += " and goods_id = ?";
            params.add(repertorySelectVO.getGoodsId());
        }
        if(repertorySelectVO.getWarehouseId() != null){
            sql += " and warehouse_id = ?";
            params.add(repertorySelectVO.getWarehouseId());
        }
        if(repertorySelectVO.getShelvesId() != null){
            sql += " and shelves_id = ?";
            params.add(repertorySelectVO.getShelvesId());
        }
        if(repertorySelectVO.getMin() != null){
            sql += " and total >= ?";
            params.add(repertorySelectVO.getMin());
        }
        if (repertorySelectVO.getMax() != null){
            sql += " and total <= ?";
            params.add(repertorySelectVO.getMax());
        }

        sql +=" group by goods_id";
        sql +=" limit "+ (pageIndex-1)*pageSize+" , "+pageSize;

        System.out.println(sql);
        return DaoUtil.querySelectAll(sql,new StockDetail(),params.toArray());
    }

    @Override
    public List<Stock> selectStock(RepertorySelectVO repertorySelectVO, Integer pageIndex, Integer pageSize) {
    //由于库存信息表中没有货架信息，所以先查库存信息明细表，然后分组，然后使用分组后的商品编号再查询库存信息


        String sql = "select goods_id from stock_detail  where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        //通过传入进来的对象动态得到sql

        //如果有商品名字则先查到名字对应的id
        if (repertorySelectVO.getGoodsName() != null){
            sql += "and goods_id = (select id  from goods_info where goods_name = ?)";
            params.add(repertorySelectVO.getGoodsName());
        }
        if(repertorySelectVO.getGoodsId() != null){
            sql += " and goods_id = ?";
            params.add(repertorySelectVO.getGoodsId());
        }
        if(repertorySelectVO.getWarehouseId() != null){
            sql += " and warehouse_id = ?";
            params.add(repertorySelectVO.getWarehouseId());
        }
        if(repertorySelectVO.getShelvesId() != null){
            sql += " and shelves_id = ?";
            params.add(repertorySelectVO.getShelvesId());
        }
        if(repertorySelectVO.getMin() != null){
            sql += " and total >= ?";
            params.add(repertorySelectVO.getMin());
        }
        if (repertorySelectVO.getMax() != null){
            sql += " and total <= ?";
            params.add(repertorySelectVO.getMax());
        }

        sql +=" group by goods_id";
        sql +=" limit "+ (pageIndex-1)*pageSize+" , "+pageSize;


        List<String> strings =  DaoUtil.querySelectStringAll(sql,params.toArray());
        //根据查询后的得到库存信息
        String selctSql = "select * from stock where goods_id in (";

        for (String string : strings) {
            selctSql+= "? ,";
        }
        selctSql = selctSql.substring(0,selctSql.length()-1);
        selctSql+=")";
        if(strings.size() == 0){
            selctSql = "select * from stock where goods_id in(-1)";
        }
        List<Stock>  stocks = DaoUtil.querySelectAll(selctSql,new Stock(),strings.toArray());


         return  stocks;
    }

    @Override
    public List<StockDetail> getStocks(Integer pageIndex, Integer pageSize, Integer id) {
       String sql = "select * from stock_detail where goods_id = ? limit "+(pageIndex-1)*pageSize+" , "+pageSize;

        return DaoUtil.querySelectAll(sql,new StockDetail(),id);
    }

    @Override
    public int totalCount(RepertorySelectVO repertorySelectVO, Integer pageIndex, Integer pageSize) {
        //由于库存信息表中没有货架信息，所以先查库存信息明细表，然后分组，然后使用分组后的商品编号再查询库存信息


        String sql = "select count(*) from stock_detail  where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        //通过传入进来的对象动态得到sql

        //如果有商品名字则先查到名字对应的id
        if (repertorySelectVO.getGoodsName() != null){
            sql += "and goods_id = (select id  from goods_info where goods_name = ?)";
            params.add(repertorySelectVO.getGoodsName());
        }
        if(repertorySelectVO.getGoodsId() != null){
            sql += " and goods_id = ?";
            params.add(repertorySelectVO.getGoodsId());
        }
        if(repertorySelectVO.getWarehouseId() != null){
            sql += " and warehouse_id = ?";
            params.add(repertorySelectVO.getWarehouseId());
        }
        if(repertorySelectVO.getShelvesId() != null){
            sql += " and shelves_id = ?";
            params.add(repertorySelectVO.getShelvesId());
        }
        if(repertorySelectVO.getMin() != null){
            sql += " and total >= ?";
            params.add(repertorySelectVO.getMin());
        }
        if (repertorySelectVO.getMax() != null){
            sql += " and total <= ?";
            params.add(repertorySelectVO.getMax());
        }

        sql +=" group by goods_id";

        String newSql = "select count(*) from ("+  sql +") as a";
        return DaoUtil.querySelectCount(newSql,params.toArray());
    }



}
